package com.czf.company.service;

import com.czf.common.entity.CompanyBaseInfo;
import com.czf.common.entity.RecordUserBank;
import com.czf.common.mapper.CompanyBaseInfoMapper;
import com.czf.common.mapper.RecordUserBankMapper;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

/**
 * @author zfChen
 * @create 2022/4/25 9:43
 */
@Service
public class CompanyScoreServiceImpl {

    @Autowired
    private CompanyBaseInfoMapper companyBaseInfoMapper;
    @Autowired
    private RecordUserBankMapper recordUserBankMapper;


    static List<String> natureOfEnterpriseList = Arrays.asList(new String[]{"国有企业", "集体所有制", "联营企业", "三资企业", "私营企业"});
    static List<String> registeredCapitalList = Arrays.asList(new String[]{"10——100万", "100——500万", "500——1000万", "1000——5000万", "5000万以上"});
    static List<String> numberOfEmployeesList = Arrays.asList(new String[]{"20人以下", "20——50人", "50——100人", "100——500人", "500人以上"});
    static List<String> areaCoveredList = Arrays.asList(new String[]{"200平米以内", "200——1000平米", "1000——5000平米", "5000——10000平米", "10000平米以上"});
    static List<String> liquidityList = Arrays.asList(new String[]{"50万以下", "50——100万", "100——500万", "500——1000万", "1000——5000万","5000万以上"});
    static List<String> totalAssetsList = Arrays.asList(new String[]{"100万以下", "100——500万", "500——1000万", "1000——5000万", "5000万——1亿","1亿以上"});

    public float getCompanyScore(Long userId) {
        float score = 0;
        CompanyBaseInfo companyBaseInfo = companyBaseInfoMapper.selectById(userId);
        if (ObjectUtils.isNotEmpty(companyBaseInfo)) {
            String natureOfEnterprise = companyBaseInfo.getNatureOfEnterprise();
            for (int i = 0; i < natureOfEnterpriseList.size(); i++) {
                if (natureOfEnterpriseList.get(i).equals(natureOfEnterprise)) {
                    score += natureOfEnterpriseList.size() - i;
                    break;
                }
            }
            String enterpriseType = companyBaseInfo.getEnterpriseType();
            if ("股份有限公司".equals(enterpriseType)) {
                score += 2;
            } else {
                score += 1;
            }
            String registeredCapital = companyBaseInfo.getRegisteredCapital();
            for (int i = 0; i < registeredCapitalList.size(); i++) {
                if (registeredCapitalList.get(i).equals(registeredCapital)) {
                    score += i + 1;
                    break;
                }
            }
            String numberOfEmployees = companyBaseInfo.getNumberOfEmployees();
            for (int i = 0; i < numberOfEmployeesList.size(); i++) {
                if (numberOfEmployeesList.get(i).equals(numberOfEmployees)) {
                    score += i + 1;
                    break;
                }
            }
            String areaCovered = companyBaseInfo.getAreaCovered();
            for (int i = 0; i < areaCoveredList.size(); i++) {
                if (areaCoveredList.get(i).equals(areaCovered)) {
                    score += i + 1;
                    break;
                }
            }
            String operatingStatus = companyBaseInfo.getOperatingStatus();
            if ("在营".equals(operatingStatus)) {
                score += 3;
            } else if ("停业".equals(operatingStatus)) {
                score += 1;
            }
            Boolean hasList = companyBaseInfo.getHasList();
            if (hasList) {
                score += 2;
            } else {
                score += 1;
            }
            Boolean hasBranchOffice = companyBaseInfo.getHasBranchOffice();
            if (hasBranchOffice) {
                score += 1;
            }
            String liquidity = companyBaseInfo.getLiquidity();
            for (int i = 0; i < liquidityList.size(); i++) {
                if (liquidityList.get(i).equals(liquidity)) {
                    score += i + 1;
                    break;
                }
            }
            String totalAssets = companyBaseInfo.getTotalAssets();
            for (int i = 0; i < totalAssetsList.size(); i++) {
                if (totalAssetsList.get(i).equals(totalAssets)) {
                    score += i + 1;
                    break;
                }
            }
        }
        return (float) (score*2.5);
    }

    public int getBankScore(Long userId) {
        RecordUserBank recordUserBank = recordUserBankMapper.selectById(userId);
        if (ObjectUtils.isNotEmpty(recordUserBank)) {
            Integer good = recordUserBank.getGoodBank();
            Integer bad = recordUserBank.getBadBank();
            if (ObjectUtils.isEmpty(good)) {
                good = 0;
            }
            if (ObjectUtils.isEmpty(bad)) {
                bad = 0;
            }
            if (bad >= 2) {
                return 0;
            } else if (bad == 0 && good >= 6) {
                return 100;
            } else if (bad == 0 && good >= 2 && good <= 5) {
                return 50 + 5 * good;
            } else if (good >= 6) {
                BigDecimal b = new BigDecimal((float) (good - 2 * bad) / good);
                Double result = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                return (int) (result * 100);
            }
        }
        return 0;
    }
}
